package logic

import (
	"context"

	"github.com/comfygpt-backend-go/common/model"
	"github.com/comfygpt-backend-go/common/utils"
	"github.com/comfygpt-backend-go/service/component-rpc/component"
	"github.com/comfygpt-backend-go/service/component-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateComponentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateComponentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateComponentLogic {
	return &UpdateComponentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateComponentLogic) UpdateComponent(in *component.ComponentItem) (*component.Empty, error) {
	// 调用数据库更新数据
	newData := &model.Components{
		Id:          in.Id,
		Name:        in.Name,
		Description: in.Description,
		Category:    in.Category,
		Icon:        in.Icon,
		Tags:        utils.ArrayToString(in.Tags),
	}

	err := l.svcCtx.ComponModel.Update(l.ctx, newData)
	if err != nil {
		return &component.Empty{}, err
	}

	return &component.Empty{}, nil
}
